<?php
namespace Sfsn\Core\Util;

use Sfsn\Core\Exception\Exception;

/**
 *
 * @author shooke
 *        
 *         语言包
 *         实例
 *         $config = [
 *         'LANG_PACK_FILE' => 'Lang',
 *         'LANG_PACK_PATH' => './Lang/',
 *         'LANG_PACK_SUFFIX' => '.php'
 *         ];
 *         Lang::init($config);
 *         //一维数组
 *         echo Lang::get('key');
 *         //二维数组
 *         echo Lang::get('key.key2');
 *         //指定语言包
 *         echo Lang::get('pack/key');
 *         echo Lang::get('pack/key.key2');
 *        
 *        
 *        
 *        
 */
class Lang
{

    /**
     * 配置
     *
     * @var unknown
     */
    public static $config = [
        'LANG_PACK_FILE' => 'zh',
        'LANG_PACK_PATH' => './Lang/',
        'LANG_PACK_SUFFIX' => '.php'
    ];

    public static $lang = [];

    /**
     * 初始化
     *
     * @param unknown $config            
     */
    public static function init($config = [])
    {
        isset($config['LANG_PACK_FILE']) && self::$config['LANG_PACK_FILE'] = $config['LANG_PACK_FILE']; // 默认语言
        isset($config['LANG_PACK_PATH']) && self::$config['LANG_PACK_PATH'] = $config['LANG_PACK_PATH']; // 语言包目录
        isset($config['LANG_PACK_SUFFIX']) && self::$config['LANG_PACK_SUFFIX'] = $config['LANG_PACK_SUFFIX']; // 语言文件后缀
        
        if (substr(self::$config['LANG_PACK_PATH'], - 1) != "/") {
            self::$config['LANG_PACK_PATH'] .= "/";
        }
    }

    /**
     * 获取不同语言的值
     *
     * @param unknown $key            
     * @return unknown
     */
    public static function get($key)
    {
        $lang = $key;
        // 获取语言文件
        $pack = self::pack($key);
        // 语言path
        $langLevel = self::langLevel($key);
        // 载入语言包
        $langArray = self::_loadLangPack($pack);
        
        $level = count($langLevel);
        switch ($level) {
            case 1:
                if (isset($langArray[$langLevel[0]])) {
                    $lang = $langArray[$langLevel[0]];
                }
                break;
            case 2:
                if (isset($langArray[$langLevel[0]][$langLevel[1]])) {
                    $lang = $langArray[$langLevel[0]][$langLevel[1]];
                }
                break;
            case 3:
                if (isset($langArray[$langLevel[0]][$langLevel[1]][$langLevel[2]])) {
                    $lang = $langArray[$langLevel[0]][$langLevel[1]][$langLevel[2]];
                }
                break;
            case 4:
                if (isset($langArray[$langLevel[0]][$langLevel[1]][$langLevel[2]][$langLevel[3]])) {
                    $lang = $langArray[$langLevel[0]][$langLevel[1]][$langLevel[2]][$langLevel[3]];
                }
                break;
            case 5:
                if (isset($langArray[$langLevel[0]][$langLevel[1]][$langLevel[2]][$langLevel[3]][$langLevel[4]])) {
                    $lang = $langArray[$langLevel[0]][$langLevel[1]][$langLevel[2]][$langLevel[3]][$langLevel[4]];
                }
                break;
            case 6:
                if (isset($langArray[$langLevel[0]][$langLevel[1]][$langLevel[2]][$langLevel[3]][$langLevel[4]][$langLevel[5]])) {
                    $lang = $langArray[$langLevel[0]][$langLevel[1]][$langLevel[2]][$langLevel[3]][$langLevel[4]][$langLevel[5]];
                }
        }
        return $lang;
    }

    /**
     * 获得语言层级
     *
     * @param unknown $string            
     * @return array
     */
    public static function langLevel($string)
    {
        $seat = strripos($string, '/');
        if ($seat === false) {
            // 不存在'/'则直接处理字符串
            $lang = explode('.', $string);
        } else {
            // 存在'/'截取字符串
            $lang = explode('.', substr($string, $seat + 1));
        }
        return $lang;
    }

    /**
     * 获取语言包文件
     *
     * @param unknown $string            
     * @return Ambigous <string, string, multitype:string , unknown>
     */
    public static function pack($string)
    {
        $pack = self::$config['LANG_PACK_FILE'];
        $seat = strripos($string, '/');
        if ($seat) {
            $pack = substr($string, 0, $seat);
        }
        return $pack;
    }

    /**
     * 加载语言包,返回数组
     */
    private static function _loadLangPack($pack)
    {
        $lang = [];
        // 哈希串
        $hash = md5($pack);
        // 如果已存在语言包直接返回
        if (self::$lang[$hash])
            return self::$lang[$hash];
            // 语言包路径
        $file = self::$config['LANG_PACK_PATH'] . $pack . self::$config['LANG_PACK_SUFFIX'];
        if (file_exists($file)) {
            // 语言包存在则载入
            $lang = require_once ($file);
            self::$lang[$hash] = is_array($lang) ? $lang : [];
            return self::$lang[$hash];
        } else {
            throw new Exception('未找到语言包' . $pack);
        }
    }
}

